<?php

class Groups extends La_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model(array("User_model", "Group_model"));
		if (!$this->authorize->is_admin_logged_in()) {
			la_redirect("/admin/login", "请先登录");
		}
		$this->data['nav'] = 'group';
	}
	
	function index() {
		echo "It works";
	}	
	/**
	 * 用户信息验证（后台那个PPT的9、10、11三页）
	 */
	public function group_verify() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$ids = $this->input->post('ids');
			for ($i = 0, $n = count($ids); $i < $n; $i++) {
				$this->Group_model->update_status($ids[$i], 1);
			}
			la_redirect("console/groups/group_verify", "保存成功");
		} else {
			$offset = intval($this->uri->segment(4));
			$groups_count = $this->Group_model->get_count_by_condition(" status = 0");
			$this->load->library('pager');
			$config['base_url'] = site_url("console/groups/group_verify/");
			$config['total_rows'] = $groups_count;
			$config['per_page'] = LA_PAGE_SIZE;
			$config['uri_segment'] = 4;
			$this->pager->initialize($config);
			$groups = $this->Group_model->find_groups_by_condition("g.status = 0", $offset, $config['per_page']);
			$this->data['groups'] = $groups;
			$this->data['pager'] =  $this->pager->create_links();
			$this->load->view('admin/group_verify', $this->data);
		}
		
	}
	
	public function allowed() {
		$offset = intval($this->uri->segment(4));
		$groups_count = $this->Group_model->get_count_by_condition(" status = 1");
		$this->load->library('pager');
		$config['base_url'] = site_url("console/groups/group_verify/");
		$config['total_rows'] = $groups_count;
		$config['per_page'] = LA_PAGE_SIZE;
		$config['uri_segment'] = 4;
		$this->pager->initialize($config);
		$groups = $this->Group_model->find_groups_by_condition("g.status = 1", $offset, $config['per_page']);
		$this->data['groups'] = $groups;
		$this->data['pager'] =  $this->pager->create_links();
		$this->load->view('admin/group_allowed', $this->data);
	}
	
	public function group_close() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$name = $this->input->post("name");
			$group = $this->Group_model->find_by_name($name);
			if (empty($group)) {
				la_redirect("console/groups/group_verify", "群组不存在");
			}
			$this->Group_model->update_status($group['id'], -1);
			$data['type'] = $this->input->post("type");
			$data['target_type'] = "gc";
			$data['target_id'] = $group['id'];
			$data['content'] = $this->input->post("content");
			$data['created_by'] = $this->authorize->current_admin_user();
			$data['created'] = date("Y-m-d H:i:s");
			$this->load->model("Feedback_model");
			$fid = $this->Feedback_model->add($data);
			la_redirect("console/groups/allowed", "操作成功");
		} else {
			$group_id = $this->input->get("group_id");
			$this->data['group'] = $this->Group_model->find_by_id($group_id);
			$this->load->view('admin/group_close', $this->data);
		}
		
	}
	
	public function closed() {
		$this->load->model("Feedback_model");
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$ids = $this->input->post('ids');
			
			for ($i = 0, $n = count($ids); $i < $n; $i++) {
				$this->Group_model->update_status($ids[$i], 1);
				$this->Feedback_model->delete_by_target_id($ids[$i], "gc");
			}
			la_redirect("console/groups/closed", "保存成功");
		} else {
			$offset = intval($this->uri->segment(4));
			$groups_count = $this->Group_model->get_count_by_condition(" status = -1");
			$this->load->library('pager');
			$config['base_url'] = site_url("console/groups/group_verify/");
			$config['total_rows'] = $groups_count;
			$config['per_page'] = LA_PAGE_SIZE;
			$config['uri_segment'] = 4;
			$this->pager->initialize($config);
			$feeback_type_array = array('1' => '虚假信息', '2' => '交易欺诈 ', '3'=>'不当言论', '4'=>'其他');
			$groups = $this->Group_model->find_groups_by_condition("g.status = -1", $offset, $config['per_page']);
			if (!empty($groups)) {
				foreach ($groups as &$group) {
					$feedback = $this->Feedback_model->find_feedbacks_by_target_id($group['id'],'gc');
					$group['feedback_type'] = $feeback_type_array[$feedback[0]['type']];
				}
			}
			unset($group);
			$this->data['groups'] = $groups;
			$this->data['pager'] =  $this->pager->create_links();
			$this->load->view('admin/closed_groups', $this->data);
		}
	}
}